﻿@{
    var companys = ViewData["Companys"] as List<Wicresoft.UnifyShow.Business.SiteBranchModel>;
    var isGlobalUser = (bool)ViewData["IsGlobalUser"];
    var myCompany = ViewData["CurrentCompany"] as Wicresoft.UnifyShow.Business.SiteBranchModel;
}
<form>
    <div id="searchParam" style="display: inline-block; *display: inline;">
        <div>
            <div class="left">查询单位</div>
            <div class="right">
                <select data-source="Company">
                    @if (!isGlobalUser)
                    { 
                        <option value="@myCompany.Name">@myCompany.Name</option>
                    }
                    else
                    {
                        foreach (var item in companys)
                        { 
                        <option value="@item.Name">@item.Name</option>
                        }
                    }
                </select>
            </div>
        </div>
        <div>
            <div class="left">创建时间从</div>
            <div class="right">
                <input type="text" class="easyui-datebox" data-source="Submit_Date_From" />
            </div>
        </div>
        <div>
            <div class="left">到</div>
            <div class="right">
                <input type="text" class="easyui-datebox" data-source="Submit_Date_To" />
            </div>
        </div>
        <div>
            <div class="left">二级单位</div>
            <div class="right">
                <select data-source="Department">
                    <option value="">全部</option>
                    <option value="省局信息中心">省局信息中心</option>
                    <option value="省局机关运维">省局机关运维</option>
                    <option value="厦门海晟技术支持组">厦门海晟技术支持组</option>
                </select>
            </div>
        </div>
        <div>
            <div class="left">查询维度</div>
            <div class="right">
                <select data-source="GroupType">
                    <option value="Department" data-groupname="单位">单位</option>
                    <option value="Name" data-groupname="个人">个人</option>
                </select>
            </div>
        </div>
        <div class="one-line">
            <div class="left">查询指标</div>
            <div class="right" style="height: auto;">
                <div class="easyui-panel" style="padding: 5px">
                    <ul class="easyui-tree" id="queryTarget" data-options="animate:true,checkbox:true">
                        <li data-options="state:'closed',iconCls:'icon-add',attributes:{tableName:'QueryEvent'}">
                            <span>事件</span>
                            <ul>
                                <li data-checked="true" data-column="Event_Count" data-parent="QueryEvent"><span>事件总数量</span></li>
                                <li data-column="Event_ResolveCount" data-parent="QueryEvent"><span>事件解决数量</span></li>
                                <li data-column="Event_ResolveTime" data-parent="QueryEvent"><span>事件平均解决时间(H)</span></li>
                                <li data-column="Event_ResolveEfficiency" data-parent="QueryEvent"><span>事件解决率(%)</span></li>
                                <li data-column="Event_ResolveSatisfaction" data-parent="QueryEvent"><span>事件平均用户满意度(%)</span></li>
                            </ul>
                        </li>
                        <li data-options="state:'closed',iconCls:'icon-add',attributes:{tableName:'QueryChange'}">
                            <span>变更</span>
                            <ul>
                                <li data-checked="true" data-column="Change_Count" data-parent="QueryChange"><span>变更总数量</span></li>
                                <li data-column="Change_ResolveCount" data-parent="QueryChange"><span>变更已实施数量</span></li>
                                <li data-column="Change_SuccessEfficiency" data-parent="QueryChange"><span>变更成功实施率(%)</span></li>
                                <li data-column="Change_ResolveEfficiency" data-parent="QueryChange"><span>变更完成率(%)</span></li>
                                <li data-column="Change_InTimeEfficiency" data-parent="QueryChange"><span>变更按时完成率(%)</span></li>
                            </ul>
                        </li>
                        <li data-options="state:'closed',iconCls:'icon-add',attributes:{tableName:'QueryTask'}">
                            <span>计划任务</span>
                            <ul>
                                <li data-checked="true" data-column="Task_Count" data-parent="QueryTask"><span>计划任务总数量</span></li>
                                <li data-column="Task_CompletedCount" data-parent="QueryTask"><span>计划任务已完成数量</span></li>
                                <li data-column="Task_ResolveEfficiency" data-parent="QueryTask"><span>计划任务完成率(%)</span></li>
                                <li data-column="Task_InTimeEfficiency" data-parent="QueryTask"><span>计划任务按时完成率(%)</span></li>
                            </ul>
                        </li>
                        <li data-options="state:'closed',iconCls:'icon-add',attributes:{tableName:'QueryQuestion'}">
                            <span>问题</span>
                            <ul>
                                <li data-checked="true" data-column="Question_Count" data-parent="QueryQuestion"><span>问题总数量</span></li>
                                <li data-column="Question_ClosedCount" data-parent="QueryQuestion"><span>已关闭问题数量</span></li>
                                <li data-column="Question_InCompleteCount" data-parent="QueryQuestion"><span>未完成问题数量</span></li>
                            </ul>
                        </li>
                        <li data-options="state:'closed',iconCls:'icon-add',attributes:{tableName:'QueryKnowledge'}">
                            <span>知识库</span>
                            <ul>
                                <li data-checked="true" data-column="Knowledge_Count" data-parent="QueryKnowledge"><span>文章数量</span></li>
                            </ul>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        @*<div class="one-line">
            <div class="left">查询范围</div>
            <div class="right">
                <input type="checkbox" name="searchArea" id="event" checked="checked" data-source="QueryEvent" data-field="EventCount" />
                <label for="event">事件</label>
                <input type="checkbox" name="searchArea" id="change" checked="checked" data-source="QueryChange" data-field="ChangeCount" />
                <label for="change">变更</label>
                <input type="checkbox" name="searchArea" id="plan" checked="checked" data-source="QueryTask" data-field="TaskCount" />
                <label for="plan">任务</label>
                <input type="checkbox" name="searchArea" id="question" checked="checked" data-source="QueryQuestion" data-field="QuestionCount" />
                <label for="question">问题</label>
                <input type="checkbox" name="searchArea" id="knowledge" checked="checked" data-source="QueryKnowledge" data-field="KnowledgeCount" />
                <label for="knowledge">知识库</label>
            </div>
        </div>*@
        <div>
            <div class="left">
                <button type="button" onclick="searchWorkOrderList();" data-groupname="单位">查询</button>
                <button type="reset">重置</button>
            </div>
            <div class="right"></div>
        </div>
    </div>
</form>
<div class="reportListContainer" style="position: relative; top: 0;">
    <div class="configButton" style="height: 22px; padding: 10px;" data-querytype="合并工单">
        <iframe name="exportFrame" id="exportFrame" src="/ReportView/ExportReportViewToExcel?rnd=workorderlist" border="0" frameborder="0" style="border: none; height: 27px; vertical-align: middle; display: none;"></iframe>
    </div>
    <div class="reportList padding5" style="height: 600px; min-height: 400px;">
        <div style="height: 90%;">
            <table id="searchGroupList"></table>
        </div>
    </div>
</div>


<script type="text/javascript">
    var relations = [];
    relations["福建省烟草专卖局"] = ['省局信息中心', '省局机关运维', '厦门海晟技术支持组'];
    relations["三明市烟草专卖局"] = ['三明信息中心', '厦门海晟技术支持组'];
    relations["龙岩市烟草专卖局"] = ['龙岩信息中心', '厦门海晟技术支持组'];
    relations["南平市烟草专卖局"] = ['南平信息中心', '厦门海晟技术支持组'];
    relations["莆田市烟草专卖局"] = ['莆田信息中心', '厦门海晟技术支持组'];
    relations["厦门市烟草专卖局"] = ['厦门信息中心', '厦门海晟技术支持组'];
    relations["宁德市烟草专卖局"] = ['宁德信息中心', '厦门海晟技术支持组'];
    relations["泉州市烟草专卖局"] = ['泉州信息中心', '厦门海晟技术支持组'];
    relations["漳州市烟草专卖局"] = ['漳州信息中心', '厦门海晟技术支持组'];
    relations["福州市烟草专卖局"] = ['福州信息中心', '厦门海晟技术支持组'];
    relations["三明金叶复烤有限公司"] = ['三明金叶复烤信息中心', '厦门海晟技术支持组'];
    relations["中国烟草福建进出口该公司"] = ['进出口信息中心', '厦门海晟技术支持组'];
    relations["福建武夷烟叶有限公司"] = ['武夷烟叶信息中心', '厦门海晟技术支持组'];
    relations["福建烟草海晟投资管理有限公司"] = ['海投信息中心', '厦门海晟技术支持组'];

    var getDepartments = function () {

        $('[data-source="Department"]').attr('disabled', true);
        enableButton(false);
        $('#exportFrame').hide();

        var company = $('[data-source="Company"]').val(),
            department = relations[company],
            html = '<option value="">全部</option>';

        $.ajax({
            url: '/ReportView/GetDepartmentListWithCompany',
            data: { company: company },
            contentType: 'application/json',
            success: function (res) {
                if (res) {
                    var departs = res.departments;

                    $.each(Array.union(department, departs), function () {
                        var val = this, display = this;
                        if (this == '信息中心' && company != '福建省烟草专卖局') {
                            return;
                        }

                        if (/信息中心$/.test(this) && company != '福建省烟草专卖局') {
                            val = '信息中心';
                        }

                        html += '<option value="' + val + '">' + this + '</option>';
                    });
                    $('[data-source="Department"]').removeAttr('disabled').html(html);
                    enableButton(true);
                    $('#exportFrame').show();
                }
            }
        });
    };

    $(function () {
        $('#queryTarget ul > li').each(function () {
            var options = "iconCls:'icon-empty'";
            var attributes = "attributes:{";
            if ($(this).attr('data-checked') == "true") {
                options += ",checked:true"
            }

            if ($(this).attr('data-column')) {
                attributes += "column:'" + $(this).attr('data-column') + "'";
            }

            if ($(this).attr('data-parent')) {
                attributes += ",parent:'" + $(this).attr('data-parent') + "'";
            }

            options += ("," + attributes + "}");
            $(this).attr('data-options', options);
        });

        $('#queryTarget').tree({
            onExpand: function (node) {
                $('#queryTarget').tree('update', {
                    target: node.target,
                    iconCls: 'icon-remove'
                });
            },
            onCollapse: function (node) {
                $('#queryTarget').tree('update', {
                    target: node.target,
                    iconCls: 'icon-add'
                });
            }
        });

        $('.easyui-datebox').datebox({
            formatter: function (date) {
                return date.formate('yyyy-MM-dd')
            }
        });

        getDepartments();
        //单位与二级单位联动
        $('[data-source="Company"]').bind('change', function () {
            getDepartments();
        });

        $('[data-source="GroupType"]').bind('change', function () {
            $('[data-groupName]').attr('data-groupName', $(this).find('option:selected').text());
        });

        $('.foot').css({ top: document.documentElement.scrollHeight - $('.foot').height() });
    });

    var DepartmentFormatte = function (value, row, index) {
        if (value == "信息中心") {
            var company = $('[data-source="Company"]').val();
            if (company != "福建省烟草专卖局") {
                var relation = relations[company];
                if (relation && relation.length) value = relation[0];
            }
        }
        return value;
    };

    var searchWorkOrderList = function () {

        enableButton(false);
        $('#exportFrame').hide();

        var container = $('#searchParam');
        var query = ReportViewInsp.getQueryData(container);

        var columns = [
            { field: "Department", title: "二级单位", width: 120, sortable: true, formatter: DepartmentFormatte }
        ];

        if (query.GroupType == "Name") {
            columns.push({
                field: "Name", title: "姓名", width: 120, sortable: true
            });
        }

        var nodes = $('#queryTarget').tree('getChecked');
        if (nodes.length == 0) {
            $('#searchGroupList').datagrid({
                columns: [columns]
            });
            return;
        }

        $.each(nodes, function (i, node) {
            if (node && node.iconCls == "icon-empty") {
                var parent = node.attributes.parent;
                if (parent) {
                    query[parent] = query[parent] || true;
                }
                columns.push({
                    field: node.attributes.column, title: node.text, width: 150, sortable: true
                });
            }
        });

        $('#searchGroupList').datagrid({
            url: '/ReportView/SearchWorkOrderList',
            fit: true,
            queryParams: query,
            singleSelect: true,
            columns: [columns],
            sortName: 'Department',
            sortOrder: 'asc',
            onLoadSuccess: function () {
                $('#exportFrame').hide();
                if ($('#searchGroupList').datagrid('getData').total > 0) {
                    $('#exportFrame').show();
                }
                enableButton(true);
            }
        });

        var p2 = $('#searchGroupList').datagrid('getPager');
        $(p2).pagination({
            beforePageText: '第',
            afterPageText: '页&nbsp;共&nbsp;{pages}&nbsp;页',
            displayMsg: '当前显示{from} - {to}&nbsp;条记录&nbsp;共&nbsp;{total}&nbsp;条记录'

        });

    };

</script>
